﻿function AT2_OnEvent(event, ...)
	local arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9,arg10=select(1,...)
	if (event=="ADDON_LOADED" and arg1=="Blizzard_TradeSkillUI") then
	--Hook the tradeskill button
		local tradeskillFunc = function()
			if AT2_UI_EditBox:HasFocus() then
				AT2_UI_EditBox:Insert(GetTradeSkillListLink())
			elseif AT2_UI_SavedMessages_EditBox:HasFocus() then
				AT2_UI_SavedMessages_EditBox:Insert(GetTradeSkillListLink())
			end
		end
		TradeSkillLinkButton:HookScript("OnClick",tradeskillFunc)
	end
	if AT2_Channel then
		if ((strsub(event,10):lower() == AT2_Channel:lower()) or (strsub(event,10) == "CHANNEL" and AT2_Channel == AT2_UI_CCB6Text:GetText() and strfind(arg9,AT2_ExternalChannel))) then
		AT2_Print("Found event ("..event..", "..arg2..", "..arg9..") (remain: "..AT2_MessageModeRemain..")","debug","AT2_OnEvent")
			if (AT2_Activated==1 and AT2_Mode == "2") then
				AT2_MessageModeAutoSend(AT2_MessageModeRemain, arg2)
			elseif (AT2_MeasuringMessages==1) then
				AT2_MessagesInAMinute=AT2_MessagesInAMinute+1
			end
		end
	end
end

function AT2_MeasureMessages(starttime)
	if (starttime) then
		AT2_MeasureStartTime=starttime
	end
	AT2_UI_StatusTextLeft:SetText("A megadott csatornán üzenetek számolása... Eddig "..AT2_MessagesInAMinute)
	AT2_UI_StatusTextRight:SetText("Még "..AT2_TotalTime(60-(time()-AT2_MeasureStartTime)).." van hátra")
	if (AT2_MeasureStartTime+60 > time()) then
		AT2_Wait(1, AT2_MeasureMessages)
	else
	
		AT2_MeasuringMessages=0
		if (AT2_Mode == "1") then
		AT2_UI_StatusTextLeft:SetText("");AT2_UI_StatusTextRight:SetText("");
		if (AT2_MessagesInAMinute > 100) then AT2_temp_usedmeasuring = 100 elseif AT2_MessagesInAMinute == 0 then AT2_temp_usedmeasuring = 1 else  AT2_temp_usedmeasuring=AT2_MessagesInAMinute end
			AT2_temp_measuredtime = math.ceil((101-AT2_temp_usedmeasuring)*4.85+115)
			AT2_UI_SetStatusBar("1 perc alatt "..AT2_MessagesInAMinute.." üzenet volt a csatornán, ami "..AT2_TotalTime(AT2_temp_measuredtime).." idöközönkénti küldést jelent.");
			AT2_UI_SetTimedModeEditBox(AT2_temp_measuredtime)
		elseif (AT2_Mode =="2") then
	AT2_UI_StatusTextLeft:SetText("");AT2_UI_StatusTextRight:SetText("");
		if (AT2_MessagesInAMinute > 100) then AT2_temp_usedmeasuring = 100 else  AT2_temp_usedmeasuring=AT2_MessagesInAMinute end
			AT2_temp_measuredmsg = math.ceil(((101-AT2_temp_usedmeasuring)*1.87)+13)
			AT2_UI_SetStatusBar("1 perc alatt "..AT2_MessagesInAMinute.." üzenet volt a csatornán, ami "..AT2_temp_measuredmsg.." üzenetenkénti küldést jelent.");
			AT2_UI_MessageMode_MessageNum:SetNumber(AT2_temp_measuredmsg)
		end
			AT2_UI_SetStatusBar("","l") AT2_UI_SetStatusBar("","r") 
	end	
end


--[[ -- Meguntam hogy nem jövök rá egy sorra, inkább maradjon régi módszer
function AT2_OnEvent(event, ...)
--AT2_Print(event.." "..arg1.." "..arg2,"debug","OnEvent")
	if not(AT2_EventList) then
		AT2_EventList = {}
	end
	for i=1,#AT2_EventList,1 do -- +1 needed? test --removed, check it
		if (event == select(1, strsplit("&",AT2_EventList[i]))) then
			AT2_Print("event found, running ","debug","AT2_OnEvent")
			RunScript(getglobal(select(2, strsplit("&",AT2_EventList[i]).."(...)"))) -- ez nem működött
		end
	end
end

function AT2_AddEvent(event, func)
	if not(AT2_EventList) then
		AT2_EventList = {}
	end
	tinsert(AT2_EventList,event.."&"..tostring(func))
end

function AT2_RemoveEvent(event, func)
	if not(AT2_EventList) then
		AT2_EventList = {}
	end
	for i=1,#AT2_EventList,1 do
		if (AT2_EventList[i] == event.."&"..tostring(func)) then
			tremove(AT2_EventList,i)
		end
	end
end
]]

if not(AT2_ModuleList) then
AT2_ModuleList = {}
end
tinsert(AT2_ModuleList,"Eventhandling");